/*
 * @Description: 数组扁平化
 * @Author: BaldHead
 * @Date: 2022-03-29 14:18:24
 * @LastEditTime: 2022-03-30 17:02:42
 * @LastEditors: Please set LastEditors
 */
let arr = [
        [1, 2],
        [3, 4, 5],
        [6, 7, 8, 9, [11, 12, [14]]], 10
    ]
    // es6的方法
arr1 = arr.flat(Infinity);
// console.log(arr1);
// 转换成字符串
//toString
arr2 = arr.toString().split(",").map(item => parseFloat(item));
// console.log(arr2);
//json
arr3 = JSON.stringify(arr).replace(/(\[|\])/g, '').split(",").map(item => parseFloat(item))
    // console.log(arr3);
    //数组的some的方法 循环验证是否为数组
    // while (arr.some(item => Array.isArray(item))) {
    //     arr = [].concat(...arr);
    // }
    // console.log(arr);
function myFlat() {
    let result = [];
    _this = this;
    let fn = (arr) => {
        for (let index = 0; index < arr.length; index++) {
            const element = arr[index];
            if (Array.isArray(element)) {
                fn(element);
                continue;
            }
            result.push(element);
        }
    };
    fn(_this);
    return result;
}
Array.prototype.myFlat = myFlat;
console.log(arr.myFlat());

// 斐波那契数列
function finboncii(n) {
    if (n <= 1) return 1;
    let arr = [1, 1];
    let i = n + 1 - 2;
    while (i > 0) {
        let a = arr[arr.length - 2],
            b = arr[arr.length - 1];
        arr.push(a + b);
        i--;
    }
    return arr[arr.length - 1];
}
/*
 *@Description: 
 *输入一个整数N， 输出所有和为N的 连续正数序列
 *输入15
 *输出[[1, 2, 3, 4, 5], [4, 5, 6], [7, 8]]
 *@FuncAuthor: BaldHead
 *@CreateDate: 2022-03-29 19:44:57
 *@LastEditTime: 
 *@LastEditors: 
 */
function createArr(n, len) {
    let arr = new Array(len).fill(null);
    let temp = []
    arr[0] = n;
    arr = arr.map((item, index) => {
        if (item == null) {
            item = temp[index - 1] + 1;
        }
        temp.push(item);
        return item;
    })
    return arr;
}

function fn(count) {
    let result = [];
    // 求出中间值
    let middle = Math.ceil(count / 2);
    // 从1开始累加
    for (let i = 1; i < middle; i++) {
        //求出累多少次的和
        for (let j = 2;; j++) {
            const total = (i + (i + j - 1)) * (j / 2);
            if (total > count) {
                break;
            } else if (total == count) {
                result.push(createArr(i, j));
                break;
            }

        }

    }
    return result;
}
console.log(fn(4));
console.log(fn(6));
console.log(fn(10));
console.log(fn(15));